package com.king.service.impl;

import com.king.dao.UserRepository;
import com.king.pojo.entity.SysRole;
import com.king.pojo.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * @author liukai
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        SysUser sysUser = new SysUser();
//        sysUser.setUsername("admin");
//        sysUser.setPassword("$2a$10$MRvz/BeD7LsXNFfByECtfe7mQb21z7SNw.RwUXCAkKIc2XQ22qHg2");
//        SysRole role = new SysRole();
//        role.setRoleId("123123123");
//        sysUser.setRoles(Collections.singletonList(role));
//        return sysUser;

//
        SysUser user = userRepository.getByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        SysRole role = new SysRole();
        role.setRoleId("123123123");
        user.setRoles(Collections.singletonList(role));
        return user;
    }
}
